package com.itheima.mongo.api;

import com.itheima.dubbo.api.CommentApi;
import com.itheima.model.enums.CommentType;
import com.itheima.model.mongo.Comment;
import com.itheima.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment) {
        //获取动态信息
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //向comment设置评论人属性
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(comment);
        //动态更新
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取最新的数据
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //返回最新的数据
        return modify.statisCount(comment.getCommentType());
    }

    @Override
    public List<Comment> page(String movementId, CommentType commentType, Integer page, Integer pagesize) {
        //创建查询条件
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType")
                        .is(commentType.getType()))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public Boolean hasComment(String movementId, Long id, CommentType commentType) {
        //创建查询条件
        Criteria criteria = Criteria.where("userId").is(id)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, Comment.class);//判断数据是否存在
        return exists;
    }

    @Override
    public Integer remove(Comment comment) {
        //删除comment表的数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);

        //修改动态表的总数量
        Query moveQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.LOVE.getType()) {
            update.inc("loveCount", -1);
        } else {
            update.inc("commentCount", -1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(moveQuery, update, options, Movement.class);
        return modify.statisCount(comment.getCommentType());
    }

    @Override
    public Comment findById(Long userId) {
        //创建构造条件
        Query query = Query.query(Criteria.where("publishUserId").is(userId));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        return comment;
    }

    @Override
    public List<Comment> findUserVo(Integer page, Integer pagesize, Long userId, CommentType commentType) {
        //创建构造对象
        Criteria criteria = Criteria.where("publishUserId").is(userId)
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }


}
